import axios from 'axios'

axios.defaults.timeout = 20000;
// axios.defaults.baseURL = '/api'
axios.defaults.baseURL = 'http://192.168.0.110:10086'
import {Message} from 'element-ui';
import {Loading} from 'element-ui';
import {MessageBox} from 'element-ui';
import router from "../router";

let needLoadingRequestCount = 0
let loading

function startLoading() {
    loading = Loading.service({
        lock: true,
        text: '数据加载中',
        background: 'rgba(0,0,0,0.21)'
    })
}

function endLoading() {
    // 延迟500ms，防止网速特快加载中画面一闪而过
    setTimeout(function () {
        if (loading) loading.close()
    }, 500)
}

// 打开loading
function showFullScreenLoading() {
    if (needLoadingRequestCount === 0) {
        startLoading()
    }
    needLoadingRequestCount++
}

// 关闭loading
function tryHideFullScreenLoading() {
    if (needLoadingRequestCount <= 0) return
    needLoadingRequestCount--
    if (needLoadingRequestCount === 0) {
        endLoading()
    }
}

//http request拦截器 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    showFullScreenLoading()
    // Do something before request is sent
    let token = window.sessionStorage.getItem("tokenValue")
    if (token) {
        //将token放到请求头发送给服务器,将tokenkey放在请求头中
        let tokenName = window.sessionStorage.getItem('tokenName');
        //config.headers.tokenName = token;
        //也可以这种写法
        config.headers[tokenName] = token;
        return config;
    }

    return config;
}, function (error) {
    // Do something with request error
    tryHideFullScreenLoading()
    return Promise.reject(error);
});

function showNotice() {
    MessageBox.confirm("登录状态已过期，您可以继续留在该页面，或者重新登录", {
        title: '系统提示',
        confirmButtonText: '重新登录',
        type: 'warning',
        beforeClose(action, instance, done) {
            if (action === 'confirm') {
                sessionStorage.clear();
                router.push('/');
            }
            done()
        }
    })
}


//响应拦截器
axios.interceptors.response.use(success => {
    if (success.data.code && success.data.code === 500) {
        showNotice()
    }

    if (success.data.code && success.data.code !== 200 && success.data.code !== 500) {
        Message.warning(success.data.message);
    }
    tryHideFullScreenLoading()
    return success;

}, error => {
    if (error.response.status === 504 || error.response.status === 404) {
        Message.error({message: '服务器被吃了o(╯□╰)o'});
    } else if (error.response.status === 403) {
        Message.error({message: '权限不足,请联系管理员!'})
    } else if (error.response.status === 500) {
        Message.error({message: '尚未登录或登录过期,请登录!'});
        sessionStorage.clear();
        this.$router.replace('/');
    } else {
        if (error.response.data.message) {
            Message.error({message: error.response.data.message});
        } else {
            Message.error({message: '未知错误!'})
        }
    }
    tryHideFullScreenLoading()
    return false;
});
//
// //后期维护加请求路径
// let base ='';
//
// //传送json格式的post请求
// export const postRequest=(url,params)=>{
//   return axios({
//     method:'post',
//     url:'${base}${url}',
//     data:params
//   })
// }